package pl.register.android.database.crud;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import pl.register.android.database.tables.ProgramInGroupDBTable;
import pl.register.android.group.StudentsGroup;
import pl.register.android.lesson.LessonProgram;

import java.security.acl.Group;

/**
 * @author Mateusz Strzelecki
 * @since 2013-04-26
 */
public class ProgramInGroupCRUD {

    private SQLiteDatabase database;

    public ProgramInGroupCRUD(SQLiteDatabase database) {
        this.database = database;
    }

    public LessonProgram addProgramForGroup(LessonProgram lessonProgram, StudentsGroup studentsGroup) {
        if (lessonProgramIsExists(lessonProgram, studentsGroup)) {
            updateProgram(lessonProgram, studentsGroup);
        } else {
            insertProgram(lessonProgram, studentsGroup);
        }
        return lessonProgram;
    }

    public void insertProgram(LessonProgram lessonProgram, StudentsGroup studentsGroup) {
        ContentValues insertValues = new ContentValues();
        insertValues.put(ProgramInGroupDBTable.CLASSES_DATE_IN_MS, lessonProgram.getClassesDateInMs());
        insertValues.put(ProgramInGroupDBTable.HOMEWORK_DATE_IN_MS, lessonProgram.getHomeworkDateInMs());
        insertValues.put(ProgramInGroupDBTable.TAG, lessonProgram.getTagLesson().toString());
        insertValues.put(ProgramInGroupDBTable.FK_LESSON_PROGRAM_ID, lessonProgram.getId());
        insertValues.put(ProgramInGroupDBTable.FK_STUDENT_GROUP_ID, studentsGroup.getId());
        long rowId = database.insert(ProgramInGroupDBTable.TABLE_NAME, null, insertValues);
    }

    public void updateProgram(LessonProgram lessonProgram, StudentsGroup studentsGroup) {
        String where = ProgramInGroupDBTable.FK_LESSON_PROGRAM_ID + " = " + lessonProgram.getId() +
                " AND " +
                ProgramInGroupDBTable.FK_STUDENT_GROUP_ID + " = " + studentsGroup.getId();
        ContentValues updateValues = new ContentValues();
        updateValues.put(ProgramInGroupDBTable.TAG, lessonProgram.getTagLesson().toString());
        updateValues.put(ProgramInGroupDBTable.CLASSES_DATE_IN_MS, lessonProgram.getClassesDateInMs());
        updateValues.put(ProgramInGroupDBTable.HOMEWORK_DATE_IN_MS, lessonProgram.getHomeworkDateInMs());
        long rowId = database.update(ProgramInGroupDBTable.TABLE_NAME, updateValues, where, null);
    }

    public void deleteProgram(LessonProgram lessonProgram, StudentsGroup studentsGroup) {
        String where = ProgramInGroupDBTable.FK_LESSON_PROGRAM_ID + " = " + lessonProgram.getId() +
                " AND " +
                ProgramInGroupDBTable.FK_STUDENT_GROUP_ID + " = " + studentsGroup.getId();
        int res = database.delete(ProgramInGroupDBTable.TABLE_NAME, where, null);
    }

    private boolean lessonProgramIsExists(LessonProgram lessonProgram, StudentsGroup studentsGroup) {
        boolean isExists;
        String where = ProgramInGroupDBTable.FK_LESSON_PROGRAM_ID + " = " + lessonProgram.getId() +
                " AND " +
                ProgramInGroupDBTable.FK_STUDENT_GROUP_ID + " = " + studentsGroup.getId();
        Cursor cursor = database.query(ProgramInGroupDBTable.TABLE_NAME, null, where, null, null, null, null);
        isExists = cursor.moveToFirst();
        cursor.close();
        return isExists;
    }

}